package com.example.composestudy.router.part1.login

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavGraph
import com.example.composestudy.router.TAG
import com.example.composestudy.router.part1.home.HomeDirection2

@Composable
fun LoginPager(
    navController: NavController,
    viewModel: LoginViewModel = viewModel(),
) {
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("登录页") },
                navigationIcon = {
                    Button(onClick = {
                        navController.navigateUp()
                    }) {
                        Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "返回")
                    }
                }
            )
        },
        content = { innerPadding ->
            var age by rememberSaveable { mutableStateOf("") }
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
                modifier = Modifier
                    .fillMaxSize()
                    .padding(16.dp)
                    .padding(innerPadding)
            ) {
                TextField(
                    value = viewModel.name,
                    onValueChange = viewModel::onNameChange,
                    label = { Text("请输入姓名") },
                    colors = TextFieldDefaults.textFieldColors(
                        backgroundColor = MaterialTheme.colors.surface,
                    ),
                )
                TextField(
                    value = age,
                    onValueChange = { age = it },
                    label = { Text("请输入年龄") },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                    colors = TextFieldDefaults.textFieldColors(
                        backgroundColor = MaterialTheme.colors.surface,
                    ),
                )
                Button(onClick = {
                    navController.navigate(
                        HomeDirection2.toHomePager(HomeDirection2.User(
                            name = viewModel.name,
                            age = age.toIntOrNull() ?: 0)
                        )
                    )
                    // 展示往回带数据所以注释掉
                    /*{
                        popUpTo(RouterConfig.WELCOME) {
                            inclusive = true
                        }
                    }*/
                }) {
                    Text("进入首页")
                }
            }
            Log.d(TAG, "LoginPager: 堆栈信息：\n" +
                    "backQueue(当前后退堆栈)：${navController.backQueue.size}\n" +
                    "destinationCountOnBackStack(后退堆栈上的非导航图目标数): ${
                        navController.backQueue.count { entry ->
                            entry.destination !is NavGraph
                        }
                    }")
        }
    )
}